﻿using System;
using System.Linq;


namespace QuanLyKTX
{
    using QuanLyKTX.Utilities;
    using DAL;

    public partial class frm_Main : Telerik.WinControls.UI.RadForm
    {
        public tbl_NhanVien NVDangNhap { get; set; }

        #region Public Methods
        public void OpenFullMenu()
        {
            radMenu_QLKTX.Enabled = true;

            //radMenuItem_HopDong.Enabled = true;
            radMenuItem_PhanQuyen.Enabled = true;
            radMenuItem_ThemNguoiDung.Enabled = true;
            //radMenuItem_QuanLyThuPhi.Enabled = true;
            radMenuItem_QuanLyVaiTro.Enabled = true;
            //radMenuItem_GhiChiSo.Enabled = true;
            //radMenuItem_QuanLyPhong.Enabled = true;
            //radMenuItem_ThemPhong.Enabled = true;
        }

        public void GetPermission()
        {
            if (NVDangNhap != null)
            {
                radMenu_QLKTX.Enabled = true;                
                radMenuItem_PhanQuyen.Enabled = false;
                radMenuItem_ThemNguoiDung.Enabled = false;                
                radMenuItem_QuanLyVaiTro.Enabled = false;

                radMenuItem_HoaDonThu.Enabled = false;
                radMenuItem_LapHD.Enabled = false;
                radMenuItem_XuatHDDienNuoc.Enabled = false;
                radMenuItem_GhiCSDienNuoc.Enabled = false;
                radMenuItem_LapPhieuThu.Enabled = false;
                radMenuItem_QuanLyHD.Enabled = false;
                radMenuItem_QuyDinh.Enabled = false;
                radMenuItem_DSTreTienPhong.Enabled = false;
                radMenuItem_ThPhong.Enabled = false;
                radMenuItem_XPhong.Enabled = false;
                radMenuItem_QLSinhVien.Enabled = false;

                using (var db = new QuanLyKTXDataContext())
                {
                    var results = from p in db.tbl_PhanQuyens
                                  where p.MaVaiTro == NVDangNhap.MaVaiTro
                                  select p;
                    if (results != null)
                    {
                        foreach (var result in results)
                        {
                            switch (result.MaGiaoDien)
                            {
                                case "frm_BillExported":
                                    radMenuItem_HoaDonThu.Enabled = Convert.ToBoolean(result.Xem);
                                    break;
                                case "frm_Contracts":
                                    radMenuItem_LapHD.Enabled = Convert.ToBoolean(result.Xem);
                                    break;
                                case "frm_EWBills":
                                    radMenuItem_XuatHDDienNuoc.Enabled = Convert.ToBoolean(result.Xem);
                                    break;
                                case "frm_EWRecords":
                                    radMenuItem_GhiCSDienNuoc.Enabled = Convert.ToBoolean(result.Xem);
                                    break;
                                case "frm_IncomeBill":
                                    radMenuItem_LapPhieuThu.Enabled = Convert.ToBoolean(result.Xem);
                                    break;
                                case "frm_ListContracts":
                                    radMenuItem_QuanLyHD.Enabled = Convert.ToBoolean(result.Xem);
                                    break;
                                case "frm_PriceConfig":
                                    radMenuItem_QuyDinh.Enabled = Convert.ToBoolean(result.Xem);
                                    break;
                                case "frm_RoomBill":
                                    radMenuItem_DSTreTienPhong.Enabled = Convert.ToBoolean(result.Xem);
                                    break;
                                case "frm_Rooms":
                                    radMenuItem_ThPhong.Enabled = Convert.ToBoolean(result.Xem);
                                    break;
                                case "frm_SortRoom":
                                    radMenuItem_XPhong.Enabled = Convert.ToBoolean(result.Xem);
                                    break;
                                case "frm_StudentInfor":
                                    radMenuItem_QLSinhVien.Enabled = Convert.ToBoolean(result.Xem);
                                    break;

                            }
                        }
                    }
                }
            }
        }

        #endregion

        #region Private Methods
        private void LockMenu(bool value)
        {
            radMenu_QLKTX.Enabled = value;
        }

        private void CloseChildForm(string childrenFormName)
        {
            foreach (var cForm in MdiChildren)
            {
                if (!cForm.Name.Equals(childrenFormName))
                    cForm.Close();
            }
        }

        #endregion

        #region Event Methods

        private void radMenuItem_LapPhieuThu_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_IncomeBill).Name);
            MdiFormLoader.LoadFormType(typeof(frm_IncomeBill), this);
        }

        private void radMenuItem_QuyDinh_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_PriceConfig).Name);
            MdiFormLoader.LoadFormType(typeof(frm_PriceConfig), this);     
        }

        private void radMenuItem_XuatHDDienNuoc_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_EWBills).Name);
            MdiFormLoader.LoadFormType(typeof(frm_EWBills), this);
        }

        private void radMenuItem_DSTreTienPhong_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_RoomBill).Name);
            MdiFormLoader.LoadFormType(typeof(frm_RoomBill), this);
        }

        private void radMenuItem_HoaDonThu_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_BillExported).Name);
            MdiFormLoader.LoadFormType(typeof(frm_BillExported), this);
        }

        private void radMenuItem_QuanLyHD_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_ListContracts).Name);
            MdiFormLoader.LoadFormType(typeof(frm_ListContracts), this);
        }

        private void radMenuItem_LapHD_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_Contracts).Name);
            MdiFormLoader.LoadFormType(typeof(frm_Contracts), this);
        }

        private void radMenuItem_QLSinhVien_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_StudentInfor).Name);
            MdiFormLoader.LoadFormType(typeof(frm_StudentInfor), this);
        }
        public frm_Main()
        {
            InitializeComponent();
                       
        }

        private void frm_Main_Load(object sender, EventArgs e)
        {            
            WindowState = System.Windows.Forms.FormWindowState.Maximized;
            Activate();
        }

        private void frm_Main_Activated(object sender, EventArgs e)
        {
            if (NVDangNhap == null)
            {
                LockMenu(false);
                MdiFormLoader.LoadFormType(typeof(frm_Login), this);
            }
        }

        private void radMenuItem_QuanLyVaiTro_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_Role).Name);
            MdiFormLoader.LoadFormType(typeof(frm_Role), this);
        }

        private void radMenuItem_ThemNguoiDung_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_Employees).Name);
            MdiFormLoader.LoadFormType(typeof(frm_Employees), this);
        }

        private void radMenuItem_PhanQuyen_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_Permission).Name);
            MdiFormLoader.LoadFormType(typeof(frm_Permission), this);
        }        

        private void radMenuItem_DoiMatKhau_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_ChangePassword).Name);
            MdiFormLoader.LoadFormType(typeof(frm_ChangePassword), this);
        }        

        private void radMenuItem_ThoatChuongTrinh_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void radMenuItem_ThoatTaiKhoan_Click(object sender, EventArgs e)
        {
            NVDangNhap = null;
            CloseChildForm(typeof(frm_Login).Name);
            LockMenu(false);
            MdiFormLoader.LoadFormType(typeof(frm_Login), this);
        }

        private void radMenuItem_DoiTTCaNhan_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_PersonalInfor).Name);
            MdiFormLoader.LoadFormType(typeof(frm_PersonalInfor), this);
        }

        

        private void radMenuItem_ThPhong_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_Rooms).Name);
            MdiFormLoader.LoadFormType(typeof(frm_Rooms), this);
        }

        private void radMenuItem_GhiCSDienNuoc_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_EWRecords).Name);
            MdiFormLoader.LoadFormType(typeof(frm_EWRecords), this);
        }

        private void radMenuItem_XPhong_Click(object sender, EventArgs e)
        {
            CloseChildForm(typeof(frm_SortRoom).Name);
            MdiFormLoader.LoadFormType(typeof(frm_SortRoom), this);
        }

        private void radMenuItem_LePhiTienPhong_Click(object sender, EventArgs e)
        {

        }
        #endregion                    
        
    }
}
